import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class PosterService extends BaseService<any> {
  private readonly logger = new Logger(PosterService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: PosterService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon';         $order = "update_time desc";          ...
const field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon';
        const order = "update_time desc";

        const search_model = this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'name', 'type' ), where).field(field).order(order).append([ 'type_name' )];
        return this.pageQuery(search_model);
    }

    /**
     * 获取自定义海报列表
     * @param where
     * @param string field
     * @return */
    async getList(where = [), field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon')
    {
        const order = "update_time desc";
        return this.model.where([ [ 'site_id', "=", this.site_id ) )).withSearch([ "name", 'type' ), where).field(field).order(order).select().toArray(];
    }

    /**
     * 获取自定义海报信息
     * @param int id
     * @param string field
     * @return */
    async getInfo(int id, field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon')
    {
        return this.model.field(field).where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).findOrEmpty().toArray(];
    }

    /**
     * 添加自定义海报
     * @param data
     * @return mixed
     */
    async add(data)
    {
        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.id;
    }

    /**
     * 编辑自定义海报
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除自定义海报
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).delete(];
    }

    /**
     * 修改自定义海报启用状态
     * @param data
     * @return Poster
     */
    async modifyStatus(data)
    {
        const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * getList
   * 对应 PHP: PosterService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(where: any[], field: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: $order = "update_time desc";         return $this->model->where([ [ 'site_id', "=", $this->site_id ] ])->withSearch([ "name", 'type' ], $where)->field...
const order = "update_time desc";
        return this.model.where([ [ 'site_id', "=", this.site_id ) )).withSearch([ "name", 'type' ), where).field(field).order(order).select().toArray(];
    }

    /**
     * 获取自定义海报信息
     * @param int id
     * @param string field
     * @return */
    async getInfo(int id, field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon')
    {
        return this.model.field(field).where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).findOrEmpty().toArray(];
    }

    /**
     * 添加自定义海报
     * @param data
     * @return mixed
     */
    async add(data)
    {
        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.id;
    }

    /**
     * 编辑自定义海报
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除自定义海报
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).delete(];
    }

    /**
     * 修改自定义海报启用状态
     * @param data
     * @return Poster
     */
    async modifyStatus(data)
    {
        const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * getInfo
   * 对应 PHP: PosterService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number, field: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: return $this->model->field($field)->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();     }      /**      *...
return this.model.field(field).where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).findOrEmpty().toArray(];
    }

    /**
     * 添加自定义海报
     * @param data
     * @return mixed
     */
    async add(data)
    {
        data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.id;
    }

    /**
     * 编辑自定义海报
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除自定义海报
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).delete(];
    }

    /**
     * 修改自定义海报启用状态
     * @param data
     * @return Poster
     */
    async modifyStatus(data)
    {
        const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * add
   * 对应 PHP: PosterService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: $data[ 'site_id' ] = $this->site_id;         $res = $this->model->create($data);         return $res->id;     }      /**      * 编辑自定义海报      * @param ...
data[ 'site_id' ] = this.site_id;
        const res = this.model.create(data);
        return res.id;
    }

    /**
     * 编辑自定义海报
     * @param int id
     * @param data
     * @return bool
     */
    async edit(int id, data)
    {
        this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除自定义海报
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).delete(];
    }

    /**
     * 修改自定义海报启用状态
     * @param data
     * @return Poster
     */
    async modifyStatus(data)
    {
        const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * edit
   * 对应 PHP: PosterService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);         return true;     }      /**      * 删除自定义海报    ...
this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).update(data);
        return true;
    }

    /**
     * 删除自定义海报
     * @param int id
     * @return bool
     */
    async del(int id)
    {
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).delete(];
    }

    /**
     * 修改自定义海报启用状态
     * @param data
     * @return Poster
     */
    async modifyStatus(data)
    {
        const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * del
   * 对应 PHP: PosterService_admin::del()
   * 逻辑类型: undefined - undefined
   */
  async del(id: number) {
    // 基于PHP真实逻辑: del
    // PHP原文: return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->delete();     }      /**      * 修改自定义海报启用状态      * @param $dat...
return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).delete(];
    }

    /**
     * 修改自定义海报启用状态
     * @param data
     * @return Poster
     */
    async modifyStatus(data)
    {
        const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * modifyStatus
   * 对应 PHP: PosterService_admin::modifyStatus()
   * 逻辑类型: undefined - undefined
   */
  async modifyStatus(data: any) {
    // 基于PHP真实逻辑: modifyStatus
    // PHP原文: $poster_info = $this->model->field('is_default')->where([             [ 'id', '=', $data[ 'id' ] ],             [ 'site_id', '=', $this->site_id ]    ...
const poster_info = this.model.field('is_default').where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).findOrEmpty().toArray(];
        if (!poster_info)) throw new AdminBusinessException('POSTER_NOT_EXIST');
        if (poster_info[ 'is_default' ) == 1) throw new AdminBusinessException('POSTER_IN_USE_NOT_ALLOW_MODIFY'];
        return this.model.where([
            [ 'id', '=', data[ 'id' ] ],
            [ 'site_id', '=', this.site_id )
        )).update([ status: data[ 'status' ) )];
    }

    /**
     * 将自定义海报修改为默认海报
     * @param data
     * @return bool
     */
    async modifyDefault(data)
    {
        try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * modifyDefault
   * 对应 PHP: PosterService_admin::modifyDefault()
   * 逻辑类型: undefined - undefined
   */
  async modifyDefault(data: any) {
    // 基于PHP真实逻辑: modifyDefault
    // PHP原文: try {             $info = $this->getInfo($data[ 'id' ]);             if (empty($info)) {                 return false;             }             Db::s...
try {
            const info = this.getInfo(data[ 'id' )];
            if (!info)) {
                return false;
            }
            Db.startTrans(];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'type', '=', info[ 'type' ] ) )).update([ is_default: 0 )];
            this.model.where([ [ 'site_id', '=', this.site_id ], [ 'id', '=', data[ 'id' ] ) )).update([ is_default: 1, status: 1, update_time: time() ]];
            Db.commit(];
            return true;
  }

  /**
   * getType
   * 对应 PHP: PosterService_admin::getType()
   * 逻辑类型: undefined - undefined
   */
  async getType(type: any) {
    // 基于PHP真实逻辑: getType
    // PHP原文: return ( new CorePosterService() )->getType($type);     }      /**      * 查询海报模板      * @param string $addon      * @param string $type      * @return...
return ( this.corePosterService ).getType(type);
    }

    /**
     * 查询海报模板
     * @param string addon
     * @param string type
     * @return array|null
     */
    async getTemplateList(addon = '', type = '')
    {
        return ( this.corePosterService ).getTemplateList(addon, type);
    }

    /**
     * 获取组件列表
     * @param params 查询指定条件的海报组件
     * @return */
    async getComponentList(params = [))
    {
        const data = ComponentDict.getComponent(];
        foreach (data as k => v) {

            // 该分组下若没有组件则清空
            if (!v[ 'list' ))) {
                unset(data[ k )];
                continue;
            }

            // 查询组件支持的页面
            if (!!v[ 'support' ))) {
                if (params[ 'type' ) != k && !!params[ 'type' )) && !!v[ 'support' )) && !in_[params[ 'type' ], v[ 'support' ))) {
                    unset(data[ k )];
                    continue;
                }
            }

            const sort_arr = [];
            foreach (v[ 'list' ) as ck => cv) {
                sort_arr[] = cv[ 'sort' ];
                unset(data[ k ][ 'list' ][ ck )[ 'sort' )];
            }

            array_multisort(sort_arr, SORT_ASC, data[ k )[ 'list' )]; //排序，根据 sort 排序
  }

  /**
   * getTemplateList
   * 对应 PHP: PosterService_admin::getTemplateList()
   * 逻辑类型: undefined - undefined
   */
  async getTemplateList(addon: any, type: any) {
    // 基于PHP真实逻辑: getTemplateList
    // PHP原文: return ( new CorePosterService() )->getTemplateList($addon, $type);     }      /**      * 获取组件列表      * @param array $params 查询指定条件的海报组件      * @retur...
return ( this.corePosterService ).getTemplateList(addon, type);
    }

    /**
     * 获取组件列表
     * @param params 查询指定条件的海报组件
     * @return */
    async getComponentList(params = [))
    {
        const data = ComponentDict.getComponent(];
        foreach (data as k => v) {

            // 该分组下若没有组件则清空
            if (!v[ 'list' ))) {
                unset(data[ k )];
                continue;
            }

            // 查询组件支持的页面
            if (!!v[ 'support' ))) {
                if (params[ 'type' ) != k && !!params[ 'type' )) && !!v[ 'support' )) && !in_[params[ 'type' ], v[ 'support' ))) {
                    unset(data[ k )];
                    continue;
                }
            }

            const sort_arr = [];
            foreach (v[ 'list' ) as ck => cv) {
                sort_arr[] = cv[ 'sort' ];
                unset(data[ k ][ 'list' ][ ck )[ 'sort' )];
            }

            array_multisort(sort_arr, SORT_ASC, data[ k )[ 'list' )]; //排序，根据 sort 排序
  }

  /**
   * getComponentList
   * 对应 PHP: PosterService_admin::getComponentList()
   * 逻辑类型: undefined - undefined
   */
  async getComponentList(params: any[]) {
    // 基于PHP真实逻辑: getComponentList
    // PHP原文: $data = ComponentDict::getComponent();         foreach ($data as $k => $v) {              // 该分组下若没有组件则清空             if (empty($v[ 'list' ])) {      ...
const data = ComponentDict.getComponent(];
        foreach (data as k => v) {

            // 该分组下若没有组件则清空
            if (!v[ 'list' ))) {
                unset(data[ k )];
                continue;
            }

            // 查询组件支持的页面
            if (!!v[ 'support' ))) {
                if (params[ 'type' ) != k && !!params[ 'type' )) && !!v[ 'support' )) && !in_[params[ 'type' ], v[ 'support' ))) {
                    unset(data[ k )];
                    continue;
                }
            }

            const sort_arr = [];
            foreach (v[ 'list' ) as ck => cv) {
                sort_arr[] = cv[ 'sort' ];
                unset(data[ k ][ 'list' ][ ck )[ 'sort' )];
            }

            array_multisort(sort_arr, SORT_ASC, data[ k )[ 'list' )]; //排序，根据 sort 排序
  }

  /**
   * getInit
   * 对应 PHP: PosterService_admin::getInit()
   * 逻辑类型: undefined - undefined
   */
  async getInit(params: any[]) {
    // 基于PHP真实逻辑: getInit
    // PHP原文: $data = [             'id' => 0,             'name' => $params[ 'name' ],             'type' => $params[ 'type' ],             'channel' => '',       ...
const data = [
            id: 0,
            name: params[ 'name' ],
            type: params[ 'type' ],
            channel: '',
            value: '',
            status: 1,
            is_default: 0,
            addon: ''
        ];

        if (!!params[ 'id' ))) {
            const field = 'id, name, type, channel, value, status,is_default, addon';
            const info = this.getInfo(params[ 'id' ), field];
            if (!!info)) {
                const data = info;
            }
  }
}
